﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ReportingSystem.IRepository;
using ReportingSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ReportingSystem.Controllers
{
    /// <summary>
    /// 报修单管理
    /// </summary>
    public class RepairsController : BaseController
    {
        public RepairsController(IUserRepository userService, IRepairsRepository repairsRepository)
            : base(userService, repairsRepository)
        {
        }

        /// <summary>
        /// 根据条件查询维修记录
        /// </summary>
        /// <param name="repairs">beginTime、endTime、state（0,1）</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetAllRepairs(dynamic repairs)
        {
            int code = 0;
            string message = "";
            List<Repairs> repairslist = new List<Repairs>();
            try
            {
                repairslist = _repairsService.GetAll(repairs);
                code = 200;
                message = "成功！";
            }
            catch
            {
                code = 500;
                message = "失败！";
                return new JsonResult(new { code, message });
            };
            return new JsonResult(new { code, message , repairslist });
        }

        /// <summary>
        /// 根据条id询维修记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRepairs(string id)
        {
            int code = 0;
            string message = "";
            Repairs repairs = new Repairs();
            try
            {
                repairs = _repairsService.Get(int.Parse(id));
                code = 200;
                message = "成功！";
            }
            catch
            {
                code = 500;
                message = "失败！";
                return new JsonResult(new { code, message });
            };
            return new JsonResult(new { code, message, repairs });
        }

        /// <summary>
        /// 添加报修
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRepairs(Repairs repairs, int id)
        {

            int code = 0;
            string message = "";
            var data = "";
            try
            {
                code = 200;
                message = "提交成功！";
                var user = _userService.Get(x => x.ID == id);
                //var user1 = _userService.Get(x => x.ID == 8);
                List<User> userlist = _userService.GetAll(o => o.state == 0 & o.role == 1);
                User user2 = userlist.OrderBy(o => o.Repairss.Count).FirstOrDefault();
                //User u = _userService.Get(o => o.Repairss.Count == 0 & o.state == 0 & o.role == 1);
                List<User> users = new List<User>();
                users.Add(user);
                users.Add(user2);
                repairs.Users = users;
                _repairsService.Create(repairs);
            }
            catch
            {
                code = 500;
                message = "提交失败！";
                return new JsonResult(new { code, message, data });
            }
            return new JsonResult(new { code, message, data });
        }

        /// <summary>
        ///报修单信息修改
        /// </summary>
        /// <returns></returns>
        /// 
        [HttpPost]
        public IActionResult EditRepairs(Repairs repairs)
        {
            int code = 0;
            string message = "";
            try
            {
                Repairs newrepairs = _repairsService.Get(repairs.ID);
                newrepairs.evaluate = repairs.evaluate;
                newrepairs.result = repairs.result;
                newrepairs.state = repairs.state;
                if (repairs.state == 1)
                {
                    newrepairs.endTime = DateTime.Now;
                }
                _repairsService.Update(newrepairs);
                code = 200;
                message = "成功！";
            }
            catch
            {
                code = 500;
                message = "失败！";
                return new JsonResult(new { code, message });
            };
            return new JsonResult(new { code, message });
        }


    }
}
